package travelDream.beans;

import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import traveldream.ejb.DTO.ExcursionDTO;
import traveldream.ejb.DTO.HotelDTO;
import traveldream.ejb.DTO.PackageDTO;
import traveldream.ejb.DTO.TransportationDTO;
import traveldream.ejb.interfaces.ExcursionBeanLocal;
import traveldream.ejb.interfaces.HotelBeanLocal;
import traveldream.ejb.interfaces.NoDeleteException;
import traveldream.ejb.interfaces.NotFindException;
import traveldream.ejb.interfaces.PackageBeanLocal;
import traveldream.ejb.interfaces.TransportationBeanLocal;

@ManagedBean(name = "CheckInvalid")
@ViewScoped

public class CheckInvalid {
	
	@EJB
	private PackageBeanLocal PackageBeanLocal;
	@EJB
	private HotelBeanLocal HotelBeanLocal;
	@EJB
	private TransportationBeanLocal TransportationBeanLocal;
	@EJB
	private ExcursionBeanLocal ExcursionBeanLocal;
	
	Date d;
	
	public void checkAll(){
		d = new Date();
		checkTransportation();
		checkExcursion();
		checkPackage();
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Info", "Check Complete!"));
	}

	private void checkExcursion() {
		// TODO Auto-generated method stub
		List<ExcursionDTO> eDTO =  ExcursionBeanLocal.getAllExcursion();
		for(int i=0;i < eDTO.size();i++){
			if(eDTO.get(i).getDate().before(d)){
				try{
				ExcursionBeanLocal.delete(eDTO.get(i).getId());
				}
				catch(NotFindException e){
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Info", "Check Failed!Excursion not found. Please try again"));
				}
				catch(NoDeleteException e){
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "You cannot delete!! Please delete fisrt the package in which it is"));
				}
			}
		}
	}

	private void checkPackage() {
		// TODO Auto-generated method stub
		List<PackageDTO> pDTO = PackageBeanLocal.allPackage();
		for(int i=0;i < pDTO.size();i++){
		try{
			if(TransportationBeanLocal.find(pDTO.get(i).getIdGoing()).getDepartureDate().before(d)){
				PackageBeanLocal.delete(pDTO.get(i).getId());
			}
		}
    	catch(NotFindException e){
    		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Info", "Check Failed!Package not found. Please try again"));
    		    	}
		}	
	}

	private void checkTransportation() {
		// TODO Auto-generated method stub
		List<TransportationDTO> tDTO = TransportationBeanLocal.getAllTransport();
		for(int i=0;i < tDTO.size();i++){
			if(tDTO.get(i).getDepartureDate().before(d)){
				try{
				TransportationBeanLocal.delete(tDTO.get(i).getId());
				}
				catch(NotFindException e){
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Info", "Check Failed!Transportation mean not found. Please try again"));
				}
				catch(NoDeleteException e){
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "You cannot delete!! Please delete fisrt the package in which it is"));
				}
			}
		}
		
	}
	
}


